// eslint-disable-next-line
import * as loginService from '@/api/user'
// eslint-disable-next-line
import { BasicLayout, BlankLayout, PageView, RouteView } from '@/layouts'

// 前端路由表
const constantRouterComponents = {
  // 基础页面 layout 必须引入
  BasicLayout: BasicLayout,
  BlankLayout: BlankLayout,
  RouteView: RouteView,
  PageView: PageView,
  // 工作总览
  'Workplace': () => import('@/views/dashboard/Workplace'), // 专利管理工作台
  'ProcessWorkplace': () => import('@/views/dashboard/ProcessWorkplace'), // 流程工作台
  'TaskWorkplace': () => import('@/views/dashboard/TaskWorkplace'), // 专利师工作台
  'TrademarkWorkplace': () => import('@/views/dashboard/TrademarkWorkplace'), // 商标管理师工作台
  'DesignWorkplace': () => import('@/views/dashboard/DesignWorkplace'), // 商标管理师工作台
  'IsoWorkplace': () => import('@/views/dashboard/IsoWorkplace'), // ISO流程工作台
  'ThreeWorkplace': () => import('@/views/dashboard/ThreeWorkplace'), // 3a流程工作台
  'FourWorkplace': () => import('@/views/dashboard/FourWorkplace'), // 400流程工作台
  'GxWorkplace': () => import('@/views/dashboard/GxWorkplace'), // 高新流程工作台
  'PgWorkplace': () => import('@/views/dashboard/GxWorkplace'), // 评估流程工作台
  'OtherWorkplace': () => import('@/views/dashboard/OtherWorkplace'), // 其他流程工作台
  // 专利流程
  'BusinessReceive': () => import('@/views/patent-process/BusinessReceive'),
  'BusinessList': () => import('@/views/patent-process/BusinessList'),
  'BusinessEdit': () => import('@/views/patent-process/BusinessEdit'),
  'AdminDetail': () => import('@/views/patent-process/AdminDetail'),
  'Government': () => import('@/views/patent-process/Government'),
  'Information': () => import('@/views/patent-process/Information'),
  'TaskList': () => import('@/views/patent-process/TaskList'),
  'TaskWarning': () => import('@/views/patent-process/TaskWarning'),
  // 'TaskFeedback': () => import('@/views/patent-process/TaskFeedback'),
  'TaskCreate': () => import('@/views/patent-process/TaskCreate'),
  'TaskEdit': () => import('@/views/patent-process/TaskEdit'),
  'Natural': () => import('@/views/patent-process/Natural'),
  'TagList': () => import('@/views/patent-process/TagList'),
  'ImportLostPt': () => import('@/views/patent-process/ImportLostPt'),
  // 商标流程
  'TrademarkList': () => import('@/views/tm-process/BusinessList'),
  'TrademarkReceive': () => import('@/views/tm-process/BusinessReceive'),
  'TrademarkEdit': () => import('@/views/tm-process/BusinessEdit'),
  'TrademarkGovernment': () => import('@/views/tm-process/Government'),
  'TrademarkInformation': () => import('@/views/tm-process/Information'),
  'LicenseList': () => import('@/views/tm-process/LicenseList'),
  'WriteReceive': () => import('@/views/tm-process/WriteReceive'),
  'ImportLostTm': () => import('@/views/tm-process/ImportLostTm'),
  // 设计
  'DesignList': () => import('@/views/design-process/DesignList'),
  'DesignEdit': () => import('@/views/design-process/DesignEdit'),
  'DesignFilter': () => import('@/views/design-process/DesignFilter'),
  // ISO
  'IsoList': () => import('@/views/iso-process/BusinessList'),
  'IsoGovernment': () => import('@/views/iso-process/Government'),
  'IsoInformation': () => import('@/views/iso-process/Information'),
  'IsoEdit': () => import('@/views/iso-process/BusinessEdit'),
  // 3a
  'ThreeList': () => import('@/views/3a-process/BusinessList'),
  'ThreeGovernment': () => import('@/views/3a-process/Government'),
  'ThreeInformation': () => import('@/views/3a-process/Information'),
  'ThreeEdit': () => import('@/views/3a-process/BusinessEdit'),
  // 400
  'FourList': () => import('@/views/400-process/BusinessList'),
  'FourGovernment': () => import('@/views/400-process/Government'),
  'FourInformation': () => import('@/views/400-process/Information'),
  'FourEdit': () => import('@/views/400-process/BusinessEdit'),
  // 高新
  'GxList': () => import('@/views/gx-process/BusinessList'),
  'GxGovernment': () => import('@/views/gx-process/Government'),
  'GxInformation': () => import('@/views/gx-process/Information'),
  'GxEdit': () => import('@/views/gx-process/BusinessEdit'),
  // 评估
  'PgList': () => import('@/views/pg-process/BusinessList'),
  'PgGovernment': () => import('@/views/pg-process/Government'),
  'PgInformation': () => import('@/views/pg-process/Information'),
  'PgEdit': () => import('@/views/pg-process/BusinessEdit'),
  'PgTask': () => import('@/views/pg-process/PgTask'),
  'Appraiser': () => import('@/views/pg-process/Appraiser'),
  // 其他
  'OtherList': () => import('@/views/other-process/BusinessList'),
  'OtherGovernment': () => import('@/views/other-process/Government'),
  'OtherInformation': () => import('@/views/other-process/Information'),
  'OtherEdit': () => import('@/views/other-process/BusinessEdit'),
  // 组员管理
  'DesignMember': () => import('@/views/member/DesignMember'),
  'PatentMember': () => import('@/views/member/PatentMember'),
  'TradeMember': () => import('@/views/member/TradeMember'),
  // 数据统计
  'PatentStats': () => import('@/views/stats/PatentStats'),
  'TradeStats': () => import('@/views/stats/TradeStats'),
  'DesignStats': () => import('@/views/stats/DesignStats'),
  // 业务维护
  'PatentBusiness': () => import('@/views/common-business/PatentBusiness'),
  'DesignBusiness': () => import('@/views/common-business/DesignBusiness'),
  'TradeBusiness': () => import('@/views/common-business/TradeBusiness'),
  'IsoBusiness': () => import('@/views/common-business/IsoBusiness'),
  'PgBusiness': () => import('@/views/common-business/PgBusiness'),
  'AddBusiness': () => import('@/views/common-business/AddBusiness'),
  'EditBusiness': () => import('@/views/common-business/EditBusiness'),
  'PatentState': () => import('@/views/common-business/PatentState'),
  'TradeState': () => import('@/views/common-business/TradeState'),
  'IsoState': () => import('@/views/common-business/IsoState'),
  'PgState': () => import('@/views/common-business/PgState'),
  // 消息管理
  'MessageList': () => import('@/views/common-message/MessageList'),
  'MessageAdd': () => import('@/views/common-message/MessageAdd'),
  'MessageEdit': () => import('@/views/common-message/MessageEdit'),
  // 文章管理
  'ArticleClass': () => import('@/views/common-article/ArticleClass'),
  'ArticleClassAdd': () => import('@/views/common-article/ArticleClassAdd'),
  'ArticleClassEdit': () => import('@/views/common-article/ArticleClassEdit'),
  'ArticleList': () => import('@/views/common-article/ArticleList'),
  'ArticleAdd': () => import('@/views/common-article/ArticleAdd'),
  'ArticleEdit': () => import('@/views/common-article/ArticleEdit'),
  // 买卖管理
  'OrderDetail': () => import('@/views/tm-shop/OrderDetail'),
  'AddForm': () => import('@/views/tm-shop/AddForm'),
  'Intention': () => import('@/views/tm-shop/Intention'),
  'Purchase': () => import('@/views/tm-shop/Purchase'),
  'Wareroom': () => import('@/views/tm-shop/Wareroom'),
  'WareDetail': () => import('@/views/tm-shop/WareDetail'),
  'Cert': () => import('@/views/tm-shop/Cert'),
  'CertType': () => import('@/views/tm-shop/CertType'),

  // 主体管理
  'ApplySubject': () => import('@/views/common-apply/ApplySubject'),
  // 其他
  'ChangePassword': () => import('@/views/other/ChangePassword')
}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*', redirect: '/404', hidden: true
}

// 根级菜单
const rootRouter = {
  key: '',
  name: 'index',
  path: '',
  component: 'BasicLayout',
  redirect: '/dashboard',
  meta: { title: '系统首页' },
  children: []
}

/**
 * 动态生成菜单
 * @param token
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (token) => {
  return new Promise((resolve, reject) => {
    loginService.getNav(token).then(res => {
      const result = res.result.antd_menus
      const menuNav = []
      rootRouter.children = result
      menuNav.push(rootRouter)
      const routers = generator(menuNav)
      routers.push(notFoundRouter)
      resolve(routers)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap, parent) => {
  return routerMap.map(item => {
    const { title, show, hideChildren, hiddenHeaderContent, target, icon } = item.meta || {}
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.path || `${parent && parent.path || ''}/${item.key}`,
      // 路由名称，建议唯一
      name: item.name || item.key || '',
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.key],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: (constantRouterComponents[item.component || item.key]) || (() => import(`@/views/${item.component}`)),

      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: title,
        icon: icon || undefined,
        hiddenHeaderContent: hiddenHeaderContent,
        target: target
      }
    }
    // 是否设置了隐藏菜单
    if (show === false) {
      currentRouter.hidden = true
    }
    // 是否设置了隐藏子菜单
    if (hideChildren) {
      currentRouter.hideChildrenInMenu = true
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith('http')) {
      currentRouter.path = currentRouter.path.replace('//', '/')
    }
    // 重定向
    item.redirect && (currentRouter.redirect = item.redirect)
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generator(item.children, currentRouter)
    }
    return currentRouter
  })
}
